// pages/userlogin/userlogin.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    USER_ASSETS_IDCARD: '', //手机号
    USER_ASSETS_NAME: '',
    ressj: '',
    timer: "", // 定时器名字
    countDownNum: '60',
    verify: "",
    flag: true, //设置节流
    POST_ADD: '',
    POST_QX:'',
    wangdian: '',
    shangwang: '',
    sbwngdian: true,
    wangdiany: true,

  },

  back() {
    wx.switchTab({
      url: '../../pages/newshouye/newshouye',
    })
  },

  //登录接口
  login_a: function () {

    if (this.data.USER_ASSETS_IDCARD == ' ' || this.data.USER_ASSETS_NAME == '' || this.data.verify == ''|| this.data.POST_QX == ''|| this.data.POST_ADD == '') {
      wx.showToast({
        title: '请完善信息！',
        icon: 'none',
        duration: 1800
      })
    } else {

      wx.request({
        url: app.globalData.interface+'/ankangdabiao/API_5521_huanledachoujinagbaoming ',
        method: 'POST',
        data: {
          USER_ASSETS_IDCARD: this.data.USER_ASSETS_IDCARD,
          USER_ASSETS_NAME: this.data.USER_ASSETS_NAME,
          code: this.data.verify,
          OPEN_ID: app.globalData.openid, //用户OPEN_ID
          session_key:app.globalData.session_key,
          OPEN_UUID: app.globalData.jiami,
          EMPLOYEE_ID:this.data.POST_ADD+'_'+this.data.POST_QX,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res);
          console.log(res.data);
          this.setData({
            ressj: res.data
          })

          if (res.data.result == 'error') {
            wx.showModal({
              title: '温馨提示',
              content: res.data.data,
              success: function (res) {
                if (res.confirm) { //这里是点击了确定以后
                  console.log('用户点击确定')
                } else { //这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
          } else { //往领券跳转时跳转带数据
            wx.navigateTo({
              // url: '../../pages/weihuyancaolingqu/weihuyancaolingqu?data=' + JSON.stringify(this.data.ressj)
              url: '../../pages/xianxinkeyouli/xianxinkeyouli?data=' + JSON.stringify(this.data.ressj)

            })
          }
        }
      })
    }
  },

  //验证手机号
  mobileInput(e) {
    let value = e.detail.value.replace(/\D/g, '')
    this.setData({
      mobile: value,
    })
    var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(19[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (this.data.mobile.length == 0) {
      wx.showToast({
        title: '输入的手机号为空，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })
    } else if (this.data.mobile.length < 11) {
      wx.showToast({
        title: '手机号长度有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })

    } else if (!myreg.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号有误，请重新输入！',
        icon: 'none',
        duration: 1500
      })
      this.setData({
        mobileFormat: false,
        USER_ASSETS_IDCARD: ''
      })

    } else {
      this.setData({
        mobileFormat: true,
      })
    }
  },


  fetch: function () {
    if (this.data.flag) {
      this.data.flag = false
      if (this.data.USER_ASSETS_IDCARD == '') {
        wx.showToast({
          title: '请填写手机号！',
          icon: 'none',
          duration: 3000
        })
      } else if (this.data.session_key == "" || this.data.jiami == "" || this.data.session_key == null || this.data.jiami == null || this.data.session_key == undefined || this.data.jiami == undefined) {
        wx.showModal({
          title: '温馨提示',
          content: '数据错误请返回首页',
          success: function (res) {
            if (res.confirm) { //这里是点击了确定以后   
              console.log('用户点击确定')

              wx.switchTab({
                url: '../../pages/newshouye/newshouye',
              })

            } else { //这里是点击了取消以后
              console.log('用户点击取消')
              wx.switchTab({
                url: '../../pages/newshouye/newshouye',
              })

            }
          }
        })
      } else {
        wx.request({
          url: app.globalData.interface+'/app/API_1052_sendMassage',
          method: 'GET',
          data: {
            phone: this.data.USER_ASSETS_IDCARD,
            KEY: 'xianjgjKey9999',
            OPEN_ID: app.globalData.openid, //用户OPEN_ID
            session_key: this.data.session_key,
            OPEN_UUID: this.data.jiami,
            phone_type: '28'
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {

            if (res.data.result == 'error') {
              wx.showToast({
                title: res.data.code,
                icon: 'none',
                duration: 3000
              })
            } else {
              wx.showToast({
                title: res.data.code,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
        var _this = this;
        var countDownNum = _this.data.countDownNum; // 获取倒计时初始值
        var timer = setInterval(function () {
          countDownNum -= 1;
          _this.setData({
            countDownNum: countDownNum
          })
          if (countDownNum <= -1) {
            clearInterval(timer);
            // 取消置顶的setInterval函数将要执行的代码
            _this.setData({
              countDownNum: 60,
            })
          }
        }, 1000)

      }
      setTimeout(() => {
        this.data.flag = true
      }, 5000)
    }

  },

  wangdianshagn() {

    if (this.data.POST_ADD == '') {
      wx.showToast({
        title: '请先选择县区！',
        icon: 'none',
        duration: 1500
      })
    } else {

      this.setData({
        sbwngdian: false
      })
    }



  },
  wangshang(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.item.L4name);
    console.log(e.currentTarget.dataset.item.EMPLOYEE_ID);

    this.setData({
      POST_QX: e.currentTarget.dataset.item.L4name,
      sbwngdian: true,
      EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,


    })
  },



    //网点选择
    wangji() {
      this.setData({
        wangdiany: false
      })
    },
  
    xwang(e) {
      console.log(e);
      console.log(e.currentTarget.dataset.item.wangname);
  
      this.setData({
        POST_ADD: e.currentTarget.dataset.item.newDistrict,
        wangdiany: true,
        L3code: e.currentTarget.dataset.item.L3code,
        POST_QX:'',
      })
      this.regionwangdian()
    },
  
    region() {
      wx.request({
        url: app.globalData.interface+'/ankangdabiao/API_5515_userQueryQuXian',
        method: 'POST',
        data: {
          L2code: 61000103,
          OPEN_ID: this.data.openid,
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
  
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data.data);
          this.setData({
            wangdian: res.data.data,
          })
   
        }
      })
    },
  
  
  
    regionwangdian() {
      wx.request({
        url: app.globalData.interface+'/ankangdabiao/API_5517_userQueryWangDian',
        method: 'POST',
        data: {
          L3code: this.data.L3code,
          OPEN_ID: this.data.openid,
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
  
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res.data.data);
          this.setData({
            shangwang: res.data.data,
          })
  
        }
      })
    },
  




  onLoad: function (options) {

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(e) {
    this.setData({
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.region() 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})